#!/usr/bin/env python
# -*- coding: utf-8 -*-

# #portugues@ScoutLink 2006 JOTI Bot (slbot)
# Copyright (c) 2006, ScoutLink #portugues team
# This source is released under the New BSD License. See the LICENSE file for further
# information.

# $Author: fmobus $
# $Date: 2006-09-09 21:53:59 -0300 (Sáb, 09 Set 2006) $ 

__version__ = "$Revision: 21 $"
# $Source$

import commands
import sys
import os
import ConfigParser

class SLConfig(ConfigParser.RawConfigParser):
	__vars = {}

	def read(self,filename):
		if len(ConfigParser.RawConfigParser.read(self,filename)) == 0:
			raise 'You should provide an EXISTING configuration file'
		self.loadVars()
		self.replaceAllVars()
	
	def loadVars(self):
		self.__vars['%DIR%']  = os.path.abspath(os.curdir)
		self.__vars['%NICK%'] = self.get('main','nickname')

	def replaceAllVars(self):
		for (sections, items) in self._sections.iteritems():
			for (name, value) in items.iteritems():
				if value.find('%') >= 0:
					items[name] = self.replaceVars(value)

	def replaceVars(self,value):
		for (var, rep) in self.__vars.iteritems():
			value = value.replace(var,rep)
		return value;
